package cn.wk.controller;

import cn.wk.AjaxResult;
import cn.wk.PageList;
import cn.wk.dto.MovieDto;
import cn.wk.query.MovieQuery;
import cn.wk.service.IMovieService;
import cn.wk.domain.Movie;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/movie")
public class MovieController {
    @Autowired
    public IMovieService movieService;

    /**
     * 保存和修改公用的
     *
     * @param movie 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Movie movie) {
        try {
            if (movie.getId() != null)
                movieService.updateById(movie);
            else
                movieService.insert(movie);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            movieService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Movie get(@PathVariable("id") Long id) {
        return movieService.selectById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<Movie> list() {

        return movieService.selectList(null);
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Movie> json(@RequestBody MovieQuery query) {
        Page<Movie> page = new Page<Movie>(query.getPage(), query.getRows());
        page = movieService.selectPage(page);
        return new PageList<Movie>(page.getTotal(), page.getRecords());
    }

    /**
     * 电影服务提供今日电影查询接口，返回今日上线的电影列表
     */
    @GetMapping("/today/list")
    public AjaxResult findToday(){
        List<Movie> list = movieService.findToday();
        return AjaxResult.me().setSuccess(true).setMessage("查询成功").setResultObj(list);
    }

    /**
     * 购票服务提供电影票购买接口，用户可以通过该接口购买电影票，同时保存购票记录
     */
    @PostMapping("/buy")
    public AjaxResult goBuy(@RequestBody Movie movie){
        Movie dbMovie = movieService.goBuy(movie);
        return AjaxResult.me().setSuccess(true).setResultObj(JSONObject.toJSONString(dbMovie));
    }

    /**
     * 更新票数
     * @param movieDto
     */
    @PostMapping("/updateTicked")
    public void updateTicked(@RequestBody MovieDto movieDto){
        movieService.updateTicked(movieDto);
    }


    /**
     * 上线电影
     */
    @PostMapping("/online")
    public AjaxResult online(@RequestBody MovieDto movieDto){
        movieService.online(movieDto);
        return AjaxResult.me().setSuccess(true).setMessage("上线成功");
    }

    /**
     * 从redis中查询电影
     */
    @GetMapping("/fromRedis")
    public AjaxResult fromRedis(){
        List<Movie> movies = movieService.fromRedis();
        return AjaxResult.me().setSuccess(true).setMessage("查询成功").setResultObj(movies);
    }


    /**
     * 从redis中购票
     */
    @PostMapping("/fromRedisBuy")
    public AjaxResult fromRedisBuy(@RequestBody MovieDto movieDto){
        movieService.fromRedisBuy(movieDto);
        return AjaxResult.me().setSuccess(true).setMessage("购买成功");
    }
}
